<script type="text/javascript">
	function prepareData() {
		prepareCustomerInfo();
		prepareEntrepotInfo();
	}
	
	/*客户信息 */
	function prepareCustomerInfo() {
		<?php foreach ($this->customers as $entry): 
			echo("g_customer[".$this->escape($entry->get_id()) . "]= new CustomerInfo("
			.$this->escape($entry->get_id()) . ",'"
			.$this->escape($entry->get_name()). "','"
			.$this->escape($entry->get_address()). "','"
			.$this->escape($entry->get_phone()). "','"
			.$this->escape($entry->get_taxId()). "','"
			.$this->escape($entry->get_bank()). "','"
			.$this->escape($entry->get_account()) . "');\n");
		endforeach ?>

		$.each(g_customer, function(i,n){
			if (!$.isEmptyObject(n)) {
				var option = "<option value='" + n.id + "'>" + n.name + "</option>";
				$(option).appendTo($("select[name|=customerId]"));	
			}   
		});
	}


	
	/*仓库信息*/
	function prepareEntrepotInfo() {
		<?php $option = ""; ?>
		<?php foreach ($this->entrepots as $entry): 
			echo("g_entrepot[".$this->escape($entry->get_id()) . "]= new EntrepotInfo("
			.$this->escape($entry->get_id()) . ",'"
			.$this->escape($entry->get_name()). "','"
			.$this->escape($entry->get_address()). "','"
			.$this->escape($entry->get_contact()). "','"
			.$this->escape($entry->get_phone()). "');\n");
			$option = $option . "<option value='" . $entry->get_id() . "'>". $this->escape($entry->get_name()). "</option>";
			
		endforeach ?>
		$("select[name|=entrepotId]").append($("<?php echo($option); ?>"));
	}



	function configEvent() {
		//客户信息
		$("select[name|=customerId]").change(function(){
			try {
				var selected = g_customer[$(this).val()];
				$("#custAddress").text(selected.address);
				$("#custPhone").text(selected.phone);
				$("#bank").text(selected.bank);
				$("#taxId").text(selected.taxId);
				$("#account").text(selected.account);		
			} catch (e) {
				if (g_debug) {
					$("#debugInfo").text("configEvent:" + e.toString());
				}
			}
		});



		//仓库下拉框选择
		$("select[name|=entrepotId]").change(function(){
			var selected = g_entrepot[$(this).val()];
			$("#address").text(selected.address);
			$("#phone").text(selected.phone);
			$("#contact").text(selected.contact);
			//从服务器取得该仓库中的所有产品信息
			$.getJSON("<?php echo $this->url(
						array(
							'controller' => 'ajax',
							'action'     => 'getposition'
						),
						'default',
						true) ?>",{Action:"get",entrepotId:$(this).val()},function(data){
									g_steelInfo = data;	
									var steelOption = "<option value='dummy'>-</option>";
									var steelIds = Array();
									$.each(g_steelInfo, function(i,item){
										//重复的产品型号不再加入
										if(!steelIds[item.steel_id]) {
											steelOption = steelOption 
												+ "<option value='" + item.steel_id + "'>" + item.steel_name + "</option>";
											steelIds[item.steel_id] = true;
										}
									});//$.each
									//重置型号下拉框的内容
									$("select[name|=steel[]]").empty();
									$("select[name|=steel[]]").append($(steelOption));	
									$("select[name|=steel[]]").change();								
									
								});	//ajax callback function
			});//$("select[name|=entrepotId]").change


		//产品名称和规格下拉框联动
		$("select[name|=steel[]]").change(function(event){
			var obj = event.target;
			var id_appendix = obj.id.split("_")[1];
			
			var specOption = "<option value='dummy'>-</option>";
			var steelId = obj.value;
			var specs = Array();
			$.each(g_steelInfo, function(i,item){
				//重复的产品型号不再加入
				if(item.steel_id == steelId 
						&& !specs[item.specs]) {
					specOption = specOption 
						+ "<option value='" + item.specs + "'>" + item.specs + "</option>";
					specs[item.specs] = true;
				}
			});//$.each
			//重置型号下拉框的内容
			var specs = $("#specs_" + id_appendix);
			specs.empty();
			specs.append($(specOption));
			
			autoSelect(specs);

			specs.change();
			
			caculatePrice();
		});

		//规格和长度下拉框联动
		$("select[name|=specs[]]").change(function(event){
			var obj = event.target;
			var id_appendix = obj.id.split("_")[1];
			
			var lengthOption = "<option value='dummy'>-</option>";
			var steelId = $("#steel_" + id_appendix).val();
			var specs = obj.value;
			var density = "";
			lengths = Array();
			$.each(g_steelInfo, function(i,item){
				if(item.steel_id == steelId 
						&& item.specs == specs 
						&& !lengths[item.length]) {
					lengthOption = lengthOption 
						+ "<option value='" + item.length + "'>" + item.length + "</option>";
					density = item.density;
					lengths[item.length] = true;
				}
			});//$.each
			//显示理重
			$("#density_" + id_appendix).text(density);
			//重置长度下拉框的内容
			var len = $("#length_" + id_appendix);
			len.empty();
			len.append($(lengthOption));
			autoSelect(len);		
			len.change();

			$("#amount_" + id_appendix).val("");
			$("#weight_" + id_appendix).val("");
			$("#price_" + id_appendix).val("");
			
			caculatePrice();
		});


		//长度下拉框变化
		$("select[name|=length[]]").change(function(event){
			var obj = event.target;
			var id_appendix = obj.id.split("_")[1];
			
			var positionOption = "<option value='dummy'>-</option>";
			var steelId = $("#steel_" + id_appendix).val();
			var specs = $("#specs_" + id_appendix).val();
			var length = $("#length_" + id_appendix).val();
			positions = Array();
			$.each(g_steelInfo, function(i,item){
				if(item.steel_id == steelId 
						&& item.specs == specs 
						&& item.length == length 
						&& !positions[item.position]) {
					positionOption = positionOption 
						+ "<option value='" + item.position + "'>" + item.position + "</option>";
					positions[item.position] = true;
				}
			});//$.each
			//重置卡号下拉框的内容
			var pos = $("#position_" + id_appendix);
			pos.empty();
			pos.append($(positionOption));
			autoSelect(pos);		
			pos.change();

			$("#amount_" + id_appendix).val("");
			$("#weight_" + id_appendix).val("");
			$("#price_" + id_appendix).val("");
			
			caculatePrice();
		});
		

		//支数，件数变化
		$("input[name|=amount[]]").change(function(event){
			var obj = event.target;
			var id_appendix = obj.id.split("_")[1];
			if($("#weight_" + id_appendix).val()!="") {
				$("#weight_" + id_appendix).val("");
			}
			caculatePrice();
		});


		//实发数变化
		$("input[name|=weight[]]").change(function(event){
			var obj = event.target;
			var id_appendix = obj.id.split("_")[1];
			if($("#amount_" + id_appendix).val()!=""){
				$("#amount_" + id_appendix).val("");
			}
			caculatePrice();
		});
		
		//单价变动
		$("input[name|=price[]]").change(caculatePrice);

		//日期控件
		$("input[name|=createDate]").datepicker();
		
	}

	function startUp() {
		$("select[name|=customerId]").change();
		$("select[name|=entrepotId]").change();
		$("select[name|=steel[]]").change();
	}

	function caculatePrice() {
		var total = 0;
		try {
			$.each($("td.subtotal"), function(i,n){
				var id_appendix = $(n).attr("id").split("_")[1];	
				var subtotal = 0;
				if($("#weight_" + id_appendix).val() != "" ) {
					subtotal = $("#weight_" + id_appendix).val() * $("#price_" + id_appendix).val();
				} else {
					subtotal = parseFloat($("#density_" + id_appendix).text() * $("#amount_" + id_appendix).val() * $("#price_" + id_appendix).val());
				}
				$(n).text(subtotal);
				$("input[name|=subtotal]").val(subtotal);
				total = total + subtotal;
			});
			$("#total").text(total);
			$("input[name|=total]").val(total);
			$("#totalRmb").text(toRmb(total));
		} catch (e) {
			if (g_debug) {
				$("#debugInfo").text("caculatePrice:" + e.toString());
			}
		}
	}

	
</script>


<form enctype="application/x-www-form-urlencoded" method="post" action="/deliveryorder/create" >
<input type="hidden" name="stockinId"/>

  <table class="infotable">
	  <tr>
	   <td class="label" rowspan="2" style="width:50px;">购&nbsp;货<br>单&nbsp;位</td>
	   <td class="label" style="width:80px;">名称</td>
	   <td style="width:30%;"><select name="customerId"></select></td>
	   <td class="label" style="width:100px;">税号</td>
	   <td id="taxId"></td>
	  </tr>
	  <tr>
	   <td class="label" style="width:20px;">地址、电话</td>
	   <td style="width:30%;"><span id="custAddress"></span>&nbsp;/&nbsp;<span id="custPhone"></span></td>
	   <td class="label" style="width:20px;">开户行、账号</td>
	   <td><span id="bank"></span>&nbsp;/&nbsp;<span id="account"></span></td>
	  </tr>
  </table>

<table class="infotable">
	<tr>
	    <td class="label" rowspan="2" style="width:50px;">仓&nbsp;库<br>信&nbsp;息</td>
	   	<td class="label" style="width:80px;">仓库</td>
		<td style="width:30%;"><select name="entrepotId"> </select></td>
		<td class="label" style="width:100px;">仓库地址</td>
		<td><span id="address"/></td>
	</tr>
	<tr>
		<td class="label" style="width:80px;">联系人</td>
		<td><span id="contact"/></td>
		<td class="label" style="width:80px;">电话</td>
		<td><span id="phone"/></td>
	</tr>
</table>

<!--清单-->
<div id="deliveryorder_detail">
  <table class="infotable">
    <tr>
     <td class="label" >品名</td>
     <td class="label">规格</td>
     <td class="label">理重</td>
     <td class="label">长度</td>
     <td class="label">卡号</td>
     <td class="label">支数、件数</td>
     <td class="label">实发数量</td>
     <td class="label">单价（元/吨）</td>
     <td class="label">结算金额</td>
     <td class="label" style="width:30%">备注说明</td>
    </tr>
    <?php for ($i = 1; $i <= 5; $i++) { ?>   
	<tr>	 
     <td>
       <input type="hidden" name="dodId[]" />
       <input type="hidden" name="subtotal[]" />
       <select name="steel[]" id="steel_<?php echo($i);?>">
       </select>
     </td>
     <td>
     	<select name="specs[]" id="specs_<?php echo($i);?>">
       </select>
     </td>
     <td id="density_<?php echo($i);?>"></td>
     <td >
     	<select name="length[]" id="length_<?php echo($i);?>"></select>
     </td>
     <td >
     	<select name="position[]" id="position_<?php echo($i);?>"></select>
     </td>
     <td><input type="text" size="5" name="amount[]" id="amount_<?php echo($i);?>"></input></td>
     <td><input type="text" size="5" name="weight[]" id="weight_<?php echo($i);?>"></input></td>
     <td><input type="text" size="5" name="price[]" id="price_<?php echo($i);?>"></input></td>
     <td class="subtotal"  id="st_<?php echo($i);?>"></td>
     <td><input name="comment[]" type="text" size="30" ></input></td>
    </tr>
    <?php } ?>    
    <tr>
     <td class="label">合计（人民币大写）:</td>
     <td colspan="7" id ="totalRmb" ></td>
     <td id="total"></td>
	 <td><input type="hidden" name="total"/></td>
    </tr>  
  </table>
  <table>
    <tr>
     <td width="80" class="label">结算方式</td>
     <td width="80">
      <select name="settlementMode">
        <OPTION value="1">现金</OPTION>
        <OPTION value="2">支票</OPTION>
        <OPTION value="0">其他</OPTION>
       </select>
     </td>
     <td width="80" class="label">发票</td>
     <td width="80">
      <select name="invoiceStatus">
        <OPTION value="1">未开</OPTION>
        <OPTION value="2">已开</OPTION>
       </select>
     </td>
     <td width="80" class="label">开单日期</td>
     <td with="100%"><input name="createDate" type="text" size="14" maxlength="11" value=""/></td>
     <td width="80" class="label">有效期</td>
     <td with="100%"><input type="text" name="expireDays"/></td>
    </tr>
  </table>
</div>


<input type="submit" name="submit" id="submit" value="保存" />
</form>
